package hcmus.fit.master.sevensea.util;

import hcmus.fit.master.sevensea.R;
import hcmus.fit.master.sevensea.background.Sea;
import hcmus.fit.master.sevensea.background.WaveLayer;
import hcmus.fit.master.sevensea.core.Constant;
import hcmus.fit.master.sevensea.sprite.Island;
import hcmus.fit.master.sevensea.sprite.Rubble;
import hcmus.fit.master.sevensea.sprite.Splash;
import hcmus.fit.master.sevensea.sprite.Whirl;
import hcmus.fit.master.sevensea.sprite.ship.BlackPirate;
import hcmus.fit.master.sevensea.sprite.ship.BluePirate;
import hcmus.fit.master.sevensea.sprite.ship.Cannon;
import hcmus.fit.master.sevensea.sprite.ship.Explosion;
import hcmus.fit.master.sevensea.sprite.ship.GhostShip;
import hcmus.fit.master.sevensea.sprite.ship.GoodShip;
import hcmus.fit.master.sevensea.sprite.ship.RedPirate;
import hcmus.fit.master.sevensea.sprite.ship.Ship;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Display;
import android.view.WindowManager;

public class GameUtils {
    public static Size getScreenSize(Context ctx) {
        WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        return new Size(display.getWidth(), display.getHeight());
    }

    public static List<int[]> getLevelInformation(String levelInfo) {
        List<int[]> rs = new ArrayList<int[]>();
        rs.clear();
        String str = "";
        while (levelInfo.indexOf('\n') >= 0) {
            str = levelInfo.substring(0, levelInfo.indexOf('\n') - 1);
            levelInfo = levelInfo.substring(levelInfo.indexOf('\n') + 1);

            String black = str.substring(0, str.indexOf(' '));
            str = str.substring(str.indexOf(' ') + 1);
            String red = str.substring(0, str.indexOf(' '));
            str = str.substring(str.indexOf(' ') + 1);
            String blue = str.substring(0, str.indexOf(' '));
            str = str.substring(str.indexOf(' ') + 1);
            String ghost = str;

            int[] tmp = new int[4];
            tmp[0] = Integer.parseInt(black);
            tmp[1] = Integer.parseInt(red);
            tmp[2] = Integer.parseInt(blue);
            tmp[3] = Integer.parseInt(ghost);
            rs.add(tmp);
        }
        return rs;
    }
    
    public static void loadResource(Resources appRes) {
        Sea.resource = BitmapFactory.decodeResource(appRes, R.drawable.grid);

        Bitmap img = BitmapFactory.decodeResource(appRes, R.drawable.goodship);
        GoodShip.Resource = Ship.GetResource(img, 16, 8, Constant.NUMERATOR, Constant.DENOMINATOR);
        GoodShip.NumOfStatus = 16;

        img = BitmapFactory.decodeResource(appRes, R.drawable.goodshipwhirl);
        GoodShip.ShipWhirl = Explosion.GetResource(img, 32, 1, Constant.NUMERATOR,
                Constant.DENOMINATOR);

        img = BitmapFactory.decodeResource(appRes, R.drawable.cannon);
        Cannon.Resource = Ship.GetResource(img, 1, 1, Constant.NUMERATOR, Constant.DENOMINATOR);

        img = BitmapFactory.decodeResource(appRes, R.drawable.explode);
        Explosion.Resource = Explosion.GetResource(img, 15, 1, Constant.NUMERATOR,
                Constant.DENOMINATOR);

        img = BitmapFactory.decodeResource(appRes, R.drawable.islands);
        Island.Resource = Island.GetResource(img, 4, 2, Constant.NUMERATOR, Constant.DENOMINATOR);

        img = BitmapFactory.decodeResource(appRes, R.drawable.whirl);
        Whirl.Resource = Whirl.GetResource(img, 1, 16, Constant.NUMERATOR, Constant.DENOMINATOR);

        img = BitmapFactory.decodeResource(appRes, R.drawable.pirate);
        BlackPirate.Resource = Ship.GetResource(img, 16, 8, Constant.NUMERATOR,
                Constant.DENOMINATOR);
        BlackPirate.NumOfStatus = 16;

        img = BitmapFactory.decodeResource(appRes, R.drawable.splash);
        Splash.Resource = Splash.GetResource(img, 12, 1, Constant.NUMERATOR, Constant.DENOMINATOR);

        img = BitmapFactory.decodeResource(appRes, R.drawable.rubble);
        Rubble.Resource = Rubble.GetResource(img, 12, 1, Constant.NUMERATOR, Constant.DENOMINATOR);

        img = BitmapFactory.decodeResource(appRes, R.drawable.redpirate);
        RedPirate.Resource = Ship.GetResource(img, 16, 8, Constant.NUMERATOR, Constant.DENOMINATOR);
        RedPirate.NumOfStatus = 16;

        img = BitmapFactory.decodeResource(appRes, R.drawable.bluepirate);
        BluePirate.Resource = Ship
                .GetResource(img, 16, 8, Constant.NUMERATOR, Constant.DENOMINATOR);
        BluePirate.NumOfStatus = 16;

        img = BitmapFactory.decodeResource(appRes, R.drawable.ghostship);
        GhostShip.Resource = Ship.GetResource(img, 16, 4, Constant.NUMERATOR, Constant.DENOMINATOR);
        GhostShip.NumOfStatus = 16;
        
        img = BitmapFactory.decodeResource(appRes, R.drawable.water);
        WaveLayer.resources = img;
        /*
         * 
         * 
         * 
         * Cannon.Sound = new SoundPlayer(path + "Sound/boom.wav");
         * Cannon.Sound.Load();
         * 
         * Explosion.Sound = new SoundPlayer(path + "Sound/explode.wav");
         * Explosion.Sound.Load();
         * 
         * 
         * Explosion.Sound = new SoundPlayer(path + "Sound/explode.wav");
         * Explosion.Sound.Load();
         */
    }
}
